package gui;

import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;

public class GuiView extends JFrame
{

	private static final long	serialVersionUID	= 1L;
	private int					buttonDim			= 70;
	private List<JButton>		buttons;
	private Thread				workingThread;

	public static void main(String[] args)
	{
		new GuiView(80).setVisible(true);
	}

	public GuiView(int no)
	{
		super();
		this.initialize(no);
		this.setVisible(true);

	}

	private void initialize(int no)
	{

		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		int x = (int) Math.ceil(Math.sqrt(no));
		int y = no / x;
		this.setBounds(100, 100, x * this.buttonDim, x * this.buttonDim);
		this.setTitle("Gruen - OK : Gelb - reagiert nicht : Rot : tot");
		this.buttons = new ArrayList<JButton>();

		JPanel jpanel = new JPanel();
		jpanel.setLayout(new GridLayout(x, y));
		this.add(jpanel);
		for (int i = 0; i < no; i++)
		{
			final JButton b = new JButton((i) + "");
			b.setBackground(Color.LIGHT_GRAY);
			b.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent e)
				{
					System.out.println(buttons.indexOf(b));
				}

			});
			jpanel.add(b);
			this.buttons.add(b);
		}

		this.addWindowListener(new WindowListener()
		{

			@Override
			public void windowActivated(WindowEvent e)
			{
				workingThread = new WorkingThread();
				workingThread.start();

			}

			@Override
			public void windowClosed(WindowEvent e)
			{
				if (!workingThread.isInterrupted())
					workingThread.interrupt();

			}

			@Override
			public void windowClosing(WindowEvent e)
			{
				if (!workingThread.isInterrupted())
					workingThread.interrupt();

			}

			@Override
			public void windowDeactivated(WindowEvent e)
			{
				if (!workingThread.isInterrupted())
					workingThread.interrupt();
			}

			@Override
			public void windowDeiconified(WindowEvent e)
			{
				// System.out.println("deiconfied");

			}

			@Override
			public void windowIconified(WindowEvent e)
			{
				// System.out.println("iconfied");

			}

			@Override
			public void windowOpened(WindowEvent e)
			{
				// System.out.println("window opened");

			}

		});

	}

	class WorkingThread extends Thread
	{
		public void run()
		{
			int i = 0;
			while (!isInterrupted())
			{

				System.out.println("thread");
				JButton b = buttons.get(i);
				b.setBackground(new Color((int) ((Math.random() * 100000000) % 256), (int) ((Math.random() * 100000000) % 256), (int) ((Math.random() * 100000000) % 256)));
				try
				{
					sleep(1000);
				} catch (InterruptedException e)
				{
					break;
				}

				i++;
				if (i > (buttons.size() - 1))
				{
					i = 0;
				}
			}
		}
	}
}
